package com.finance.model.msg;

import com.alibaba.fastjson.JSONObject;
import com.finance.model.msg.dto.MessageRequestDto;
import com.finance.model.msg.dto.MessageResponseDto;
import com.finance.pojo.BorrowerInfo;
import okhttp3.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @Author: Apollo
 * @Date: 2021/4/17
 * @Description: 发送验证码
 */
@Service
public class Message {


    @Resource
    private OkHttpClient okHttpClient;


    /**
     * 发送消息
     */
    public void send(BorrowerInfo borrowerInfo, String randomString) throws IOException {
        //OkHttpClient okHttpClient = new OkHttpClient();
        //请求构造器
        Request.Builder builder = new Request.Builder();
        //请求路径
        builder.url(MessageRequestDto.SERVICE_URL);
        //请求头部内容
        builder.header(MessageRequestDto.AUTHORIZATION_HEADER_KEY, MessageRequestDto.APP_CODE_PRE+ MessageRequestDto.APP_CODE);
        //请求体部内容
        FormBody.Builder formBodyBuilder = new FormBody.Builder();
        MessageRequestDto messageRequestDto = new MessageRequestDto();
        messageRequestDto.setMobile(borrowerInfo.getPhone());
        messageRequestDto.setParam("code:"+randomString);
        messageRequestDto.setTpl_id("TP1711063");
        formBodyBuilder.add(MessageRequestDto.COL_MOBILE,messageRequestDto.getMobile());
        formBodyBuilder.add(MessageRequestDto.COL_PARAM,messageRequestDto.getParam());
        formBodyBuilder.add(MessageRequestDto.COL_TPL_ID,messageRequestDto.getTpl_id());
        FormBody formBody = formBodyBuilder.build();
        builder.post(formBody);
        Request request = builder.build();
        //客户端执行
        Call call = okHttpClient.newCall(request);
        Response response = call.execute();
        String jsonString = response.body().string();
        //处理json字符串
        MessageResponseDto messageResponseDto = JSONObject.parseObject(jsonString, MessageResponseDto.class);
        System.out.println(messageResponseDto);
    }







//    public static void main(String[] args) throws IOException {
//        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
//        annotationConfigApplicationContext.scan("org.wyk.dto");
//        //发送消息
//        send();
//    }



}
